package com.beemindz.miyotee.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils {

  public static boolean isOnline(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
      return true;
    }
    return false;
  }

  public static JSONObject getJSONFromUrl(String url) {
    JSONObject jObj = null;
    String json = "";
    try {
      // defaultHttpClient
      DefaultHttpClient httpClient = new DefaultHttpClient();
      httpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
      httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");

      HttpGet httpGet = new HttpGet(url);
      HttpResponse httpResponse = httpClient.execute(httpGet);
      HttpEntity httpEntity = httpResponse.getEntity();
      json = EntityUtils.toString(httpEntity, HTTP.UTF_8);
      jObj = new JSONObject(json);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return jObj;

  }

  public static JSONObject postJSONObjFromUrlTemp(String url, String[] key, String[] value) {
    JSONObject jObj = null;
    String json = "";
    try {
      // defaultHttpClient
      DefaultHttpClient httpClient = new DefaultHttpClient();
      httpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
      httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
      HttpPost httppost = new HttpPost(url);

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(value.length);
      for (int i = 0; i < value.length; i++) {
        nameValuePairs.add(new BasicNameValuePair(key[i], value[i]));
      }
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse httpResponse = httpClient.execute(httppost);
      HttpEntity httpEntity = httpResponse.getEntity();
      json = EntityUtils.toString(httpEntity, HTTP.UTF_8);
      jObj = new JSONObject(json);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return jObj;

  }

  public static JSONArray postJSONListFromUrl(String url, String[] key, String[] value) {
    JSONArray jObj = null;
    String json = "";
    try {
      // defaultHttpClient
      DefaultHttpClient httpClient = new DefaultHttpClient();
      httpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
      httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
      HttpPost httppost = new HttpPost(url);

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(value.length);
      for (int i = 0; i < value.length; i++) {
        nameValuePairs.add(new BasicNameValuePair(key[i], value[i]));
      }
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse httpResponse = httpClient.execute(httppost);
      HttpEntity httpEntity = httpResponse.getEntity();
      json = EntityUtils.toString(httpEntity, HTTP.UTF_8);
      jObj = new JSONArray(json);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return jObj;

  }

  public static JSONObject getJSONObjFromUrlSSL(String url) throws Exception {
    JSONObject jObj = null;
    try {
      HttpGet httpGet = new HttpGet(url);
      HttpResponse httpResponse;

      KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
      trustStore.load(null, null);

      SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
      sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

      HttpParams params = new BasicHttpParams();
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
      HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

      SchemeRegistry registry = new SchemeRegistry();
      registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
      registry.register(new Scheme("https", sf, 443));

      ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
      DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);

      httpResponse = httpClient.execute(httpGet);
      HttpEntity httpEntity = httpResponse.getEntity();
      String json = EntityUtils.toString(httpEntity, HTTP.UTF_8);
      jObj = new JSONObject(json);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return jObj;
  }

  public static JSONObject postJSONObjFromUrl(String url, String[] key, String[] value) {
    JSONObject jObj = null;
    try {
      HttpPost httpPost = new HttpPost(url);
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(value.length);
      for (int i = 0; i < value.length; i++) {
        nameValuePairs.add(new BasicNameValuePair(key[i], value[i]));
      }
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

      HttpResponse httpResponse;

      KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
      trustStore.load(null, null);

      SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
      sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

      HttpParams params = new BasicHttpParams();
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
      HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

      SchemeRegistry registry = new SchemeRegistry();
      registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
      registry.register(new Scheme("https", sf, 443));

      ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
      DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);

      httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      String json = EntityUtils.toString(httpEntity, HTTP.UTF_8);
      jObj = new JSONObject(json);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return jObj;
  }

  public static class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");

    public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException,
        KeyStoreException, UnrecoverableKeyException {
      super(truststore);

      TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
          return null;
        }
      };

      sslContext.init(null, new TrustManager[] { tm }, null);
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
        UnknownHostException {
      return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
      return sslContext.getSocketFactory().createSocket();
    }
  }

}
